/********************************
 creater:
 create time:2016-01-14
 describe：加减号调整数量标签指令
 modify time:
 ********************************/
/*网络图片拦截器*/
APP.directive('inputNumberDirective', ['PopupService', function (PopupService) {
  return {
    restrict: 'EA',
    replace: true,
    template: '<div class="row row-no-padding text-align-c"> ' +
    '<div class="col dhcfont border bg-background color-text-m" style="height: 22px;" ng-click="OnCountMinus();"> &#xe61d; </div> ' +
    '<div class="col-50 border-top-bottom overflow-h"><input class="text-align-c horizontal-center color-text-m" style="width:50%;height: 20px;" ng-model="count" type="number" ng-change="changeStock();" ng-blur="judgeStock();"/> </div>' +
    '<div class="col dhcfont border bg-background color-text-m" style="height: 22px;" ng-click="OnCountAdd();">&#xe61c; </div> ' +
    '</div>',
    scope: {
      min: '=min',//非必填 最小值
      minText: '=minText',//非必填 最小值提示语
      max: '=max',//非必填 最大值
      maxText: '=maxText',//非必填 最大值提示语
      count: '=count', //必填 绑定变量值
      countChange: '&countChange' //非必填 count值变化执行的方法
    },
    link: function (scope, element, attrs) {
      //初始化数据
      var initData = {
        min: 1,
        max: 99,
        minText: '数量不能小于',
        maxText: '数量不能大于'
      };
      //初始化方法
      var init = function () {
        if (typeof(scope.min) == 'undefined') {
          scope.min = initData.min;
        }
        if (typeof(scope.max) == 'undefined') {
          scope.max = initData.max;
        }
        if (typeof(scope.minText) == 'undefined') {
          scope.minText = initData.minText + initData.min;
        }
        if (typeof(scope.maxText) == 'undefined') {
          scope.maxText = initData.maxText + initData.max;
        }
      };
      //初始化数据
      init();
      /*数量改变时调用*/
      scope.changeStock = function () {
        //数量改变校验方法
        if (scope.count > scope.max) {//绑定值大于最大值时，绑定值等于最大值并提示
          PopupService.showToast(scope.maxText);
          scope.count = scope.max;
        }
        //else if (scope.count < scope.min) {//绑定值小于最小值时，绑定值等于最小值并提示
        //  PopupService.showToast(scope.minText);
        //  scope.count = scope.min;
        //}
        //指令外部方法
        scope.countChange();
      };
      /*最小值绑定*/
      scope.judgeStock = function () {
        if (scope.count < scope.min) {
          PopupService.showToast(scope.minText);
          scope.count = scope.min;
        }
        scope.countChange();
      };
      /*新增数量*/
      scope.OnCountAdd = function () {
        if (scope.count >= scope.max) {
          PopupService.showToast(scope.maxText);
        } else {
          scope.count++;
        }
        scope.countChange();
      };
      /*减少数量*/
      scope.OnCountMinus = function () {
        if (scope.count > scope.min) {
          scope.count--;
        } else {
          PopupService.showToast(scope.minText);
        }
        scope.countChange();
      };
    }
  };
}]);
